//
//  IconScrollView.m
//  NewPagedFlowViewDemo
//
//  Created by apple on 2017/7/10.
//  Copyright © 2017年 robertcell.net. All rights reserved.
//

#import "IconScrollView.h"
#import "IconCollectionViewCell.h"

@interface IconScrollView ()<UIScrollViewDelegate>

/** <#描述#> */
@property (nonatomic,strong) UIScrollView * scrollView;

/** <#描述#> */
@property (nonatomic,assign) CGFloat itemW;
/** <#描述#> */
@property (nonatomic,strong) NSMutableArray * imageViewArr;
/** <#描述#> */
@property (nonatomic,assign) UIImageView * currentImgView;
/** <#描述#> */
@property (nonatomic,strong) NSMutableArray * btnArr;

@end

@implementation IconScrollView

- (CGFloat)itemW{
    return self.bounds.size.height;
}

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        [self configScrollView];
    }
    return self;
}

- (void)setImgArr:(NSArray *)imgArr{

    
    _imgArr = imgArr;
    if (self.imageViewArr.count > 0) {
        for (UIImageView * imageView in self.imageViewArr) {
            
            [imageView.superview removeFromSuperview];
        }
        [self.imageViewArr removeAllObjects];
    }
    self.imageViewArr = [NSMutableArray array];
    self.btnArr = [NSMutableArray array];
    for (int i = 0; i < self.imgArr.count; i++) {
        CGFloat imgX = self.bounds.size.width/2 + (self.itemW * i);
        CGFloat imgY = 0;
        CGFloat imgH = self.itemW;
        CGFloat imgW = self.itemW;
        CheekVodeModel *model = imgArr[i];
        UIControl * control = [[UIControl alloc] initWithFrame:CGRectMake(imgX, imgY, imgW, imgH)];
        
        control.tag = i;
        UIImageView * imageView = [[UIImageView alloc] init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(model.photo)] placeholderImage:[UIImage imageNamed:@"llustration"]];
        [control addSubview:imageView];
        [self.scrollView addSubview:control];
        
        imageView.frame = CGRectMake(10, 10, imgH-20, imgH-20);
        [self.imageViewArr addObject:imageView];
        
        imageView.layer.cornerRadius = imageView.frame.size.height/2;
        imageView.clipsToBounds = YES;
        imageView.layer.borderWidth = 1;
        
        [control addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArr addObject:control];

    }

}
- (void)configScrollView{

    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    
    //    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.scrollEnabled = YES;
    [self addSubview:self.scrollView];
    
    
}
- (void)removeAtIndex:(NSInteger)index{
    
    if ([self.imageViewArr objectAtIndex:index]) {
        UIImageView * imgV = self.imageViewArr[index];
        [imgV.superview removeFromSuperview];
        [self.imageViewArr removeObject:imgV];
        [self updateFrame];
    }
}

- (void)updateFrame{
    
    for (int i = 0; i < self.imageViewArr.count; i++) {
        UIImageView * imageV = self.imageViewArr[i];
        UIView * control = imageV.superview;
        
        CGFloat imgX = self.bounds.size.width/2 + (self.itemW * i);
        CGFloat imgY = 0;
        CGFloat imgH = self.itemW;
        CGFloat imgW = self.itemW;
        
        control.tag = i;
        control.frame = CGRectMake(imgX, imgY, imgW, imgH);
    }
}

- (void)btnClick:(UIControl *)sender{

    if ([self.delegate respondsToSelector:@selector(iconScrollView:chooseIconAtIndex:)]) {
        [self.delegate iconScrollView:self chooseIconAtIndex:sender.tag];

    }
    

}

- (void)chooseImageWithIndex:(NSInteger)index{
    
    
    UIImageView * imgV = self.imageViewArr[index];
    
    if (self.currentImgView) {
        self.currentImgView.layer.borderColor = [UIColor clearColor].CGColor;
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = self.currentImgView.frame;
            frame.size = CGSizeMake(self.itemW-20, self.itemW-20);
            frame.origin = CGPointMake(10, 10);
            self.currentImgView.layer.cornerRadius = frame.size.height/2;
            self.currentImgView.frame = frame;
            
        }];
    }
    
    self.currentImgView = imgV;
    self.currentImgView.layer.borderColor = [UIColor redColor].CGColor;
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.currentImgView.frame;
        frame.size = CGSizeMake(self.itemW, self.itemW);
        frame.origin = CGPointMake(0, 0);
        self.currentImgView.layer.cornerRadius = frame.size.height/2;
        self.currentImgView.frame = frame;
        
    }];
    
    
    CGFloat x = index * self.itemW + self.itemW/2;
    [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    if (_imgArr) {
        self.scrollView.contentSize = CGSizeMake(self.itemW*_imgArr.count + self.bounds.size.width - self.itemW, 0);
    }
    
}

@end
